package com.vhall.component.controller.account.adm;

import com.vhall.component.controller.account.facade.adm.AdmAccountFacade;
import com.vhall.component.entity.account.AccountEntity;
import com.vhall.component.entity.account.vo.*;
import com.vhall.component.framework.common.annotation.VhallComponent;
import com.vhall.component.framework.common.constant.CommonConstant;
import com.vhall.component.framework.common.exception.BusinessException;
import com.vhall.component.plugin.common.annotation.TokenAuth;
import com.vhall.component.plugin.common.constant.ConstStr;
import com.vhall.component.plugin.common.enums.AccountStatus;
import com.vhall.component.plugin.common.enums.AccountType;
import com.vhall.component.plugin.common.enums.OperateTypeEnum;
import com.vhall.component.plugin.common.enums.UserSex;
import com.vhall.component.service.account.AccountService;
import com.vhall.component.service.annotation.OperateLog;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;

/**
 * 用户管理
 *
 * @author yongzheng.xu
 * @date 2021/4/24 21:24
 */
@VhallComponent
@TokenAuth("sys:account")
@Tag(name = "用户列表-管理端")
public class AdmAccountController implements AdmAccountFacade {

    @Autowired
    private AccountService accountService;

    @ApiOperation("查询账户信息")
    @Override
    @TokenAuth("sys:account:info")
    public AccountVO info(String id) {
        AccountEntity accountEntity = accountService.getById(id);
        if (accountEntity == null) {
            throw new BusinessException("account is not present!");
        }
        AccountVO accountVO = new AccountVO();
        BeanUtils.copyProperties(accountEntity, accountVO);
        return accountVO;
    }

    @Override
    @TokenAuth("sys:account:add")
    @OperateLog(type = OperateTypeEnum.CREATE, name = "添加数据")
    public AdmAccountAddRspVO add(@Validated AdmAccountAddVO vo) {
        // type设置默认值
        if (null == vo.getType()) {
            vo.setType(2);
        }
        AdmAccountAddRspVO result = new AdmAccountAddRspVO();
        AccountEntity account = accountService.add(vo.getPhone(), vo.getUsername(), vo.getNickname(), vo.getSex(), vo.getType());
        if (account == null) {
            throw new BusinessException("account is not present!");
        }
        BeanUtils.copyProperties(account, result);
        result.setSexStr(UserSex.getValue(account.getSex()).getMsg());
        result.setTypeStr(AccountType.getValue(account.getAccountType()).getMsg());
        result.setStatusStr(AccountStatus.getValue(account.getStatus()).getMsg());
        return result;
    }

    @Override
    @TokenAuth("sys:account:list")
    public AdmAccountListRspVO list(AdmAccountListReqVO vo) {
        // type设置默认值
        if (null == vo.getType()) {
            vo.setType(2);
        }
        if (StringUtils.isNotBlank(vo.getEndTime())) {
            String endTime = vo.getEndTime();
            if (endTime.contains(ConstStr.TIME_ZERO)) {
                endTime = endTime.replace(ConstStr.TIME_ZERO, ConstStr.TIME_FULL);
            } else {
                endTime = endTime.substring(0, 11);
                endTime = endTime + " " + ConstStr.TIME_FULL;
            }
            vo.setEndTime(endTime);
        }
        if (StringUtils.isNotBlank(vo.getKeyword())) {
            String keyword = vo.getKeyword();
            if (vo.getKeyword().contains(CommonConstant.UNDERLINE)) {
                keyword = vo.getKeyword().replace(CommonConstant.UNDERLINE, "\\" + CommonConstant.UNDERLINE);
            }
            if (vo.getKeyword().contains(CommonConstant.PERCENT_SIGN)) {
                keyword = keyword.replace(CommonConstant.PERCENT_SIGN, "\\" + CommonConstant.PERCENT_SIGN);
            }
            vo.setKeyword(keyword);
        }
        return accountService.list(vo);
    }

    @Override
    @TokenAuth("sys:account:edit")
    @OperateLog(type = OperateTypeEnum.UPDATE, name = "更新用户信息")
    public Boolean edit(AdmAccountEditReqVO vo) {
        return accountService.edit(vo);
    }

    @Override
    @TokenAuth("sys:account:status")
    @OperateLog(type = OperateTypeEnum.UPDATE, name = "修改用户状态")
    public Boolean editStatus(AdmAccountEditStatusReqVO vo) {
        return accountService.editStatus(vo);
    }

}
